#!/usr/bin/env python3
# coding: utf-8
# File: chatbot_graph.py
# Author: lhy<lhy_in_blcu@126.com,https://huangyong.github.io>
# Date: 18-10-4

from question_classifier import *
from question_parser import *
from answer_search import *

'''问答类'''


class ChatBotGraph:
    def __init__(self):
        self.classifier = QuestionClassifier()
        self.parser = QuestionPaser()
        self.searcher = AnswerSearcher()

    def chat_main(self, sent, users, senderid, webhook_url, conversationType):
        # 无实体的回复
        answer = '您好，我不太清楚你问的是什么，您可以向我询问医疗相关的问题，具体如下。\n' \
                 '1. 根据症状问疾病 \n' \
                 '2.  根据疾病名称回复疾病概述、原因、并发、宜吃等问题\n' \
                 '3.  根据药品名称问药品的 \n'
        chat = '您好，我是医药智能助理。'

        res_classify = self.classifier.classify(sent, users, senderid, webhook_url, conversationType, )
        print('res_classify', res_classify)
        if not res_classify:
            return answer
        elif res_classify == 'greet':
            return chat
        elif '无意图' in res_classify.keys():
            return res_classify['无意图']
        res_sql = self.parser.parser_main(res_classify)
        final_answers = self.searcher.search_main(res_sql)
        if not final_answers:
            return chat
        else:
            return '\n'.join(final_answers)


if __name__ == '__main__':
    handler = ChatBotGraph()
    while 1:
        question = input('用户:')
        answer = handler.chat_main(question)
        print('机器人:', answer)
